﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PRJ_LDW_Eutoliso.AccountAdmin.Servico
{
    public partial class Insert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        private void Guardar()
        {
            tolisoASPNETDBEntities dbUser = new tolisoASPNETDBEntities();
            Users user = new Users();
            try
            {
                using (eutolisodbEntities1 context = new eutolisodbEntities1())
                {
                    servico serv = new servico();
                    //eventocategoria evc = new eventocategoria();
                    //cidade ci = new cidade();


                    var _tipo = context.servicotipo.First(st => st.Nome == DropDownList1.SelectedValue);

                    var _cidade = context.cidade.First(c => c.Nome == ddlCidade.SelectedValue);

                    //var _user = dbUser.Users.First(u => u.UserId == ev.Usuario);

                    //var _queryUser = from u in dbUser.Users join evt in context.evento on u.UserId equals evt.Usuario select u.UserId;

                    //var _query = (from u in dbUser.Users join _ev in context.evento on u.UserId equals _ev.Usuario 
                    //              select new 
                    //              {
                    //                _idAspUser = u.UserId,
                    //                _idEventoCat = _ev.Usuario
                    //              });

                    //var query = from u in dbUser.Users join _ev in context.evento on u.UserId equals _ev.Usuario select u.UserId;

                    //query = _idAspUser;

                    //
                    serv.cidade1 = _cidade;
                    serv.servicotipo = _tipo;
                    //ev.Usuario = _user.UserId;
                    //ev.Usuario = 

                    //adiciona nos campos 'textboxs'

                    if (FileUploadControl.HasFile)
                    {
                        try
                        {
                            if (FileUploadControl.PostedFile.ContentType == "~/Images/jpeg")
                            {
                                if (FileUploadControl.PostedFile.ContentLength < 102400)
                                {
                                    string filename = System.IO.Path.GetDirectoryName(FileUploadControl.FileName);
                                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                                    StatusLabel.Text = filename;
                                }
                                else
                                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                            }
                            else
                                StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
                        }
                        catch (Exception ex)
                        {
                            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                        }
                    }

                    if (FileUploadControl1.HasFile)
                    {
                        try
                        {
                            if (FileUploadControl1.PostedFile.ContentType == "~/Images/jpeg")
                            {
                                if (FileUploadControl1.PostedFile.ContentLength < 102400)
                                {
                                    string filename = System.IO.Path.GetDirectoryName(FileUploadControl1.FileName);
                                    FileUploadControl1.SaveAs(Server.MapPath("~/") + filename);
                                    StatusLabel.Text = filename;
                                }
                                else
                                    StatusLabel1.Text = "Upload status: The file has to be less than 100 kb!";
                            }
                            else
                                StatusLabel1.Text = "Upload status: Only JPEG files are accepted!";
                        }
                        catch (Exception ex)
                        {
                            StatusLabel1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                        }
                    }

                    string dataAtual = Convert.ToString( DateTime.Now);

                    serv.Tituloanuncio = txtTituloAnuncio.Text;
                    serv.Valor = Convert.ToDecimal( txtValor.Text);
                    serv.Imagem = "~/Images/" + FileUploadControl.FileName;
                    serv.Imagem02 = "~/Images/" + FileUploadControl1.FileName;
                    serv.Video = txtxLinkVideo.Text;
                    serv.Dataanuncio = Convert.ToDateTime(dataAtual);   //Convert.ToDateTime(txtDate1.Text);
                    serv.Descricaogeral = txtDescricao.Text;
                    serv.Descricaoadicional = txtDescricaoAdicional.Text;


                    context.servico.Add(serv);
                    context.SaveChanges();

                    msgImgInserida.Visible = true;
                    msgImgInserida2.Visible = true;

                    Image1.ImageUrl = serv.Imagem;
                    Image1.Width = 200;
                    Image1.Height = 200;
                    Image2.ImageUrl = serv.Imagem02;
                    Image2.Width = 200;
                    Image2.Height = 200;

                    lblMsg.Visible = true;

                }

            }
            catch (Exception)
            {
                lblMsgException.Visible = true;

            }
        }

        protected void btnInserir_Click(object sender, EventArgs e)
        {
            Guardar();
        }
    }
}